package blog

import (
	"time"

	"gitee.com/yangwencheng/vblog/common"
)

type BlogSet struct {
	*common.PageData
	Items []*Blog `json:"items"`
}

type Blog struct {
	*Meta
	*CreateBlogRequest
}

func (*Blog) TableName() string {
	return "blogs"
}

type Meta struct {
	ID          int64 `json:"id" uri:"id"`
	CreatedAt   int64 `json:"createdAt"`
	UpdatedAt   int64 `json:"updatedAt"`
	PublishedAt int64 `json:"publishedAt"`
}

func NewBlog() *Blog {
	return &Blog{
		Meta:              NewMeta(),
		CreateBlogRequest: NewCreateBlogRequest(),
	}
}

func NewBlogSet() *BlogSet {
	return &BlogSet{
		Items:    []*Blog{},
		PageData: common.NewPageData(),
	}

}

func NewMeta() *Meta {
	return &Meta{
		CreatedAt: time.Now().Unix(),
		UpdatedAt: time.Now().Unix(),
	}
}

func NewCreateBlogRequest() *CreateBlogRequest {
	return &CreateBlogRequest{
		Tags: make(map[string]any),
	}
}

func NewQueryBlogRequest() *QueryBlogRequest {
	return &QueryBlogRequest{
		PageData: common.NewPageData(),
	}
}

type QueryBlogRequest struct {
	*common.PageData
	Title            string    `json:"title" form:"title"`
	Author           string    `json:"author" form:"author"`
	Status           STATUS    `json:"status" form:"status"`
	PublishedAtStart time.Time `json:"publishedAtStart" form:"publishedAtStart" time_format:"2006-01-02 15:04:05"`
	PublishedAtEnd   time.Time `json:"publishedAtEnd" form:"publishedAtEnd" time_format:"2006-01-02 15:04:05"`
}
type CreateBlogRequest struct {
	Title   string         `json:"title" gorm:"column:title" validate:"required"`
	Author  string         `json:"author" gorm:"column:author"`
	Content string         `json:"content" gorm:"column:content" validate:"required"`
	Tags    map[string]any `json:"tags" gorm:"serializer:json"`
	Status  STATUS         `json:"status" gorm:"column:status"`
}
type DeleteBlogRequest struct {
	ID int64 `json:"id" uri:"id" binding:"required" validate:"required"`
}
type ShowBlogRequest struct {
	ID int64 `json:"id" uri:"id" binding:"required" validate:"required"`
}

func (req *DeleteBlogRequest) Validate() error {
	return common.Validate(req)
}

func (req *ShowBlogRequest) Validate() error {
	return common.Validate(req)
}

func (req *CreateBlogRequest) Validate() error {
	return common.Validate(req)
}
